package com.bt7274.controller;

import com.bt7274.base.MinIOUtil;
import com.bt7274.config.MinIOConfig;
import com.bt7274.grace.result.GraceJSONResult;
import com.bt7274.grace.result.ResponseStatusEnum;
import com.bt7274.pojo.Post;
import com.bt7274.pojo.bo.CommentBO;
import com.bt7274.pojo.bo.PostBO;
import com.bt7274.pojo.vo.CommentVO;
import com.bt7274.pojo.vo.PostDetailVO;
import com.bt7274.service.CommentService;
import com.bt7274.service.PostService;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/api/comment")
public class CommentController {

    @Autowired
    private CommentService commentService;
    @Resource
    private MinIOConfig minIOConfig;

    // 新增评论
    @PostMapping("/create")
    public GraceJSONResult add(@RequestPart("commentBO") CommentBO commentBO,@RequestPart(value = "file",
            required = false) MultipartFile file) {
        try {
            String imageUrl;
            if (file != null && !file.isEmpty()) {
                String filename = file.getOriginalFilename();
                if(StringUtils.isBlank(filename)){
                    return GraceJSONResult.errorCustom(ResponseStatusEnum.FILE_UPLOAD_NULL_ERROR);
                }
                filename = "image/" + dealWithFilename(filename);
                imageUrl = MinIOUtil.uploadFile(minIOConfig.getBucketName(), filename, file.getInputStream(), true);
                if(!imageUrl.isEmpty()) {
                    commentBO.setImageUML(imageUrl);
                }
            }
            commentService.addComment(commentBO);
            return GraceJSONResult.ok();
        } catch (Exception e) {
            return GraceJSONResult.errorMsg("评论失败: " + e.getMessage());
        }
    }
    private String dealWithFilename(String filename){
        String suffixName = filename.substring(filename.lastIndexOf("."));
        String fName =filename.substring(0,filename.lastIndexOf("."));
        String uuid = UUID.randomUUID().toString();
        return fName +"-"+uuid +suffixName;
    }

    // 删除评论（根据 commentId）
    @DeleteMapping("/delete/{commentId}")
    public GraceJSONResult delete(@PathVariable String commentId) {
        try {
            commentService.deleteCommentById(commentId);
            return GraceJSONResult.ok();
        } catch (Exception e) {
            return GraceJSONResult.errorMsg("删除失败: " + e.getMessage());
        }
    }

    // 查询评论列表（分页）
    @GetMapping("/list")
    public GraceJSONResult list(@RequestParam String postId,
                                @RequestParam(defaultValue = "1") int page,
                                @RequestParam(defaultValue = "10") int pageSize) {
        try {
            List<CommentVO> comments = commentService.getCommentsByPostId(postId, page, pageSize);
            return GraceJSONResult.ok(comments);
        } catch (Exception e) {
            return GraceJSONResult.errorMsg("查询评论失败: " + e.getMessage());
        }
    }
}
